// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        armatureDisplay : { //龙骨动画
            default : null,
            type : dragonBones.ArmatureDisplay,
        },
        postionAnimation : { //位移动画
            default : null,
            type : cc.Animation,
        },
        chirpSound:{
            default:null,
            type:cc.AudioClip
        }

    },


    onLoad () {

          console.log("鹤龙骨加载")
           //龙骨动画
           this.armatureDisplay = this.node.getComponent(dragonBones.ArmatureDisplay);
           //位移动画
           this.postionAnimation = this.node.getComponent(cc.Animation);
           let  random = Math.ceil(Math.random()*6)*10000; 
           setTimeout(()=>{
            this.chirpAction()
           },random)
         
    },

    chirpAction(){
        //位移动画状态
        let animState = this.postionAnimation.getAnimationState('鹤位移');
        if(animState&&animState.isPlaying){
            this.armatureDisplay.playAnimation("鸣叫",0)
            cc.audioEngine.play(this.chirpSound,false,1)
            this.postionAnimation.pause()
            let  random = (Math.ceil(Math.random()*4)+3)*1000; 
            setTimeout(()=>{
                this.walkAction()
            },random)
        }
    },

    walkAction(){
        let animState = this.postionAnimation.getAnimationState('鹤位移');
        this.armatureDisplay.playAnimation("走路",0)
        if(animState&&animState.isPaused){
            this.postionAnimation.resume()
            let  random = Math.ceil(Math.random()*6)*10000; 
            setTimeout(()=>{
                this.chirpAction()
               },random)
        }
    },
    start () {

    },

    update (dt) {

    },
});
